//
//  JQFXHShengreeAPI.swift
//  RongYiHua
//
//  Created by 卢小艳 on 2025/7/16.
//

import Foundation
import Moya
import CryptoKit
import SwiftyJSON
import Alamofire


// 定义 API 请求的 Target
enum JQFXHShengreeAPI {
    case jqfxhShengrwwSendMsg(mobile: String)
    case jqfxhShengrxxLogin(mobile: String, code:String)
    case jqfxhCityssList
    case jqfxhLoanmApply(dic: [String: String])
    case jqfxhCheckzzinto
    case jqfxhProductaaApply(productIds:[Int])
}

// 实现 Moya 的 TargetType 协议
extension JQFXHShengreeAPI: TargetType {

    var baseURL: URL {
        return URL(string: "https://hapi.srloan.cn")!

    }
    
    var path: String {
        switch self {
        case .jqfxhShengrwwSendMsg(_):
            return "/login/captcha"
        case .jqfxhShengrxxLogin(_, _):
            return "/login/sms"
        case .jqfxhCityssList:
            return "/config/city/tree"
        case .jqfxhLoanmApply(_):
            return "/user/save/basic/info"
        case .jqfxhCheckzzinto:
            return "/user/product/check/into"
        case .jqfxhProductaaApply(_):
            return "/user/product/apply"
        }
    }
    
    
    var task: Task {
        .requestParameters(parameters: self.params, encoding: method == .get ? URLEncoding.default : JSONEncoding.default)
    }
    
    var method: Moya.Method {
        switch self {
        case .jqfxhCityssList:
            return .get
        case .jqfxhCheckzzinto:
            return .get
        default:
            return .post
        }

    }
    
    
    
    
    
    var headers: [String: String]? {

        var headers = [
            "ChannelCode": "weimiaoy", // 短信签名统一
            "PackageName": "1" + "_15", // 笙融app对应关系PackageName_渠道ID
            "Platform": "IOS",
        ]
        
        if let user = JQFXHNetworkManager.shared.jqfxhCurrentUser, !user.token.isEmpty {
            headers["Token"] = user.token
        }
        
        return headers

    }
    
}


extension JQFXHShengreeAPI : JQFXHThirdErrorqqParams {
    
    var params: [String: Any] {
        switch self {
        case .jqfxhShengrwwSendMsg(let mobile):
            return ["phone": mobile]
        case .jqfxhShengrxxLogin(let mobile, let code):
            return [
                "phone": mobile,
                "captcha": code
            ]
        case .jqfxhCityssList:
            return [:]
        case .jqfxhLoanmApply(let dic):
            return dic
        case .jqfxhCheckzzinto:
            return [:]
        case .jqfxhProductaaApply(let productIds):
            return ["productIds":productIds]
        }
    }
    
   
    func jqfxhErrorssMsg(json: JSON) -> String {
        json["message"].stringValue
    }
    
    func jqfxhErrorrrCode(json: JSON) -> Int {
        return json["code"].intValue
    }
    
    
    func mobile() -> String {
        return self.params["phone"] as? String ?? ""
    }
    
    func jqfxhIsqqError(json: JSON) -> Bool {
        return jqfxhErrorrrCode(json: json) != 200
    }
    
    
}
